package com.ibeetl.admin.console.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibeetl.admin.console.dao.LeaveMsgConsoleDao;
import com.ibeetl.admin.console.dao.UserConsoleDao;
import com.ibeetl.admin.console.exception.DeletedException;
import com.ibeetl.admin.console.exception.NoResourceException;
import com.ibeetl.admin.console.web.dto.LeaveMsgExcelExportData;
import com.ibeetl.admin.console.web.dto.UserExcelExportData;
import com.ibeetl.admin.console.web.query.UserRoleQuery;
import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.CoreUserRole;
import com.ibeetl.admin.core.entity.LeaveMsg;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.service.CoreDictService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.enums.CoreDictType;
import com.ibeetl.admin.core.util.enums.DelFlagEnum;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;

@Service
@Transactional
public class LeaveMsgConsoleService extends BaseService<CoreUser> {

	@Autowired
	LeaveMsgConsoleDao leaveMsgDao;
	
	
	@Autowired
    FileService fileService;

	@Autowired
	PasswordEncryptService passwordEncryptService;
	@Autowired
	CoreDictService coreDictService;

	
	@Autowired
	CorePlatformService platformService;
	/**
	 * 根据条件查询
	 *
	 * @param query
	 */
	public void queryByCondtion(PageQuery<LeaveMsg> query) {
		PageQuery<LeaveMsg> ret = leaveMsgDao.queryByCondtion(query);
		queryListAfter(ret.getList());
	}


	/**
	 * 根据反馈id查询一条数据
	 *
	 * @param id
	 */
	public LeaveMsg queryLeaveMsgById(Long id) {
		return leaveMsgDao.queryById(id);
	}

	/**
	 * 更新反馈 只更新不为空的字段
	 *
	 * @param leaveMsg
	 * @return
	 */
	public int updateLeaveMsg(LeaveMsg leaveMsg) {
		return leaveMsgDao.updateById(leaveMsg);
	}

	/**
	 * 删除反馈
	 *
	 * @param id
	 *            反馈id
	 */
	public void delLeaveMsg(Long id) {
		LeaveMsg leaveMsg = queryLeaveMsgById(id);
		if (leaveMsg == null) {
			throw new NoResourceException("用户反馈消息不存在!");
		}

		leaveMsgDao.deleteById(leaveMsg);
	}

	/**
	 * 批量删除反馈 
	 *
	 * @param ids
	 *            反馈id列表
	 */
	public void batchDelLeaveMsg(List<Long> ids) {
		try {
			leaveMsgDao.batchDelLeaveMsgByIds(ids);
		} catch (Exception e) {
			throw new PlatformException("批量删除用户失败", e);
		}
	}
	
	
	public List<LeaveMsgExcelExportData> queryExcel(PageQuery<LeaveMsg> query) {
		PageQuery<LeaveMsg> ret = leaveMsgDao.queryByCondtion(query);
		List<LeaveMsg> list = ret.getList();
		OrgItem orgRoot = platformService.buildOrg();
		List<LeaveMsgExcelExportData> items = new ArrayList<>();
		String msgStatus = "0";
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(LeaveMsg leaveMsg:list) {
			LeaveMsgExcelExportData leaveMsgItem = new LeaveMsgExcelExportData();
			leaveMsgItem.setId(leaveMsg.getId());
			leaveMsgItem.setContent(leaveMsg.getContent());

			leaveMsgItem.setOpenid(leaveMsg.getOpenid());
			leaveMsgItem.setPhone(leaveMsg.getPhone());
			leaveMsgItem.setEmail(leaveMsg.getEmail());
			leaveMsgItem.setType(leaveMsg.getType());
			
			leaveMsgItem.setCreateTime(sdf.format(leaveMsg.getCreateTime()));
			leaveMsgItem.setFilesPath(leaveMsg.getFilesPath());
			leaveMsgItem.setCreator(leaveMsg.getCreator());
			if(null!=leaveMsg.getDealTime()) {
				leaveMsgItem.setDealTime(sdf.format(leaveMsg.getDealTime()));
			}
			leaveMsgItem.setNote(leaveMsg.getNote());
			
			if(leaveMsg.getStatus().equals("已处理")) {
				msgStatus = "1";
			} else {
				msgStatus = "0";
			}
			CoreDict dict = coreDictService.findCoreDict(CoreDictType.LEAVEMSG_STATUS,msgStatus);
			leaveMsgItem.setStatus(dict.getName());
			
			items.add(leaveMsgItem);
			
		}
		return items;
		
	}

}
